﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UIT.ScrumTool.Client.Controller;
using UIT.ScrumTool.Client.Utilities.Team;
using System.Collections;
using UIT.ScrumTool.Client.ControlService;

namespace UIT.ScrumTool.Client.View.Project
{
    /// <summary>
    /// Interaction logic for TeamTheme.xaml
    /// </summary>
    public partial class TeamTheme : UserControl
    {
        public static ListView DragSource;
        public static Object Data;
        public static int flag = 0;

        public TeamTheme()
        {
            InitializeComponent();
        }

        private void comboBoxRole_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            switch (comboBox.SelectedIndex)
            {
                case 0:  // Scrum Master
                    comboBox.Background = System.Windows.Media.Brushes.Yellow;
                    break;
                case 1:  // Product Owner
                    comboBox.Background = System.Windows.Media.Brushes.SkyBlue;
                    break;
                case 2:  // Developer
                    comboBox.Background = System.Windows.Media.Brushes.Snow;
                    break;
                case 3:  // Tester
                    comboBox.Background = System.Windows.Media.Brushes.SpringGreen;
                    break;
                case 4:  // Visitor
                    comboBox.Background = System.Windows.Media.Brushes.YellowGreen;
                    break;
                default:
                    break;
            }

            // Update Member Info to ProjectUser Table
            ProjectUserCtrl.ProjectUserUpdateRole(comboBox.TabIndex, comboBox.SelectedIndex);
            try
            {
                MemberInfo memberInfo = ((List<MemberInfo>)listViewMemberTeam.ItemsSource).Find(mem => mem.ProjectUserId == comboBox.TabIndex);
                memberInfo.UserRole = comboBox.SelectedIndex;

            }
            catch (Exception)
            {

            }
        }

        private void listViewMemberTeam_Drop(object sender, DragEventArgs e)
        {
            ListView parent = (ListView) sender;
            object data = e.Data.GetData(typeof (MemberInfo));

            // Drag From List Member
            if (flag == 0)
            {
                uctrlProject.lstMember.Remove((MemberInfo) data);
                ((IList) uctrlProject.DragSource.ItemsSource).Remove(data);
                uctrlProject.DragSource.Items.Refresh();
            }

                // Drag From Another Team
            else
            {
                ((IList) DragSource.ItemsSource).Remove(data);
                DragSource.Items.Refresh();
            }

            //parent.Items.Add(data);

            // Update TeamId in ProjectUser Table
            ProjectUserDto projectUserDto = new ProjectUserDto();
            projectUserDto.Id = ((MemberInfo) data).ProjectUserId;
            projectUserDto.UserId = ((MemberInfo) data).UserId;
            projectUserDto.ProjectId = ((MemberInfo) data).ProjectId;
            projectUserDto.UserRole = ((MemberInfo) data).UserRole;
            projectUserDto.TeamId = parent.TabIndex;
            ProjectUserCtrl.ProjectUserUpdate(projectUserDto);


            // Update List Member for Team
            var teamInfoTarget = uctrlProject.lstTeam.Find(team => team.TeamId == projectUserDto.TeamId);
            if (teamInfoTarget != null)
            {
                if (teamInfoTarget.LstMemberTeam == null)
                {
                    teamInfoTarget.LstMemberTeam = new List<MemberInfo>();
                }
                teamInfoTarget.LstMemberTeam.Add((MemberInfo) data);

                parent.ItemsSource = teamInfoTarget.LstMemberTeam;
                parent.Items.Refresh();
            }

            if (flag == 1)
            {
                var teamInfoSource = uctrlProject.lstTeam.Find(team => team.TeamId == DragSource.TabIndex);
                if (teamInfoSource != null)
                {
                    if (teamInfoSource.LstMemberTeam != null)
                    {
                        teamInfoSource.LstMemberTeam.Remove((MemberInfo)data);
                    }
                }
            }

        }

        private void listViewMemberTeam_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListView Parent = (ListView)sender;
            DragSource = Parent;
            Data = uctrlProject.GetDataFromListView(DragSource, e.GetPosition(Parent));
        }

        private void btnDrag_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (Data != null)
                {
                    flag = 1;
                    DragDrop.DoDragDrop(DragSource, Data, DragDropEffects.Move);                    
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
